<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Recharge extends CI_Controller {
	//充值页面
	function index(){
		//$user = $this->session->userdata('ych_user');
		$headers = apache_request_headers();
		$user_id = isset($headers['user_id'])? intval($headers['user_id']) : 0;
		$token = isset($headers['token'])? $headers['token'] : '';
		$token_key = 'token_'.$user_id;
		$token_val = $this->mycache->get($token_key);
		if($user_id && $token == $token_val){
			//$this->load->model('User_model', 'user');
			//$user = $this->user->get_by_uid($user_id);
			//$ret['user'] = $user;
			
			if (time()>=strtotime('2014-04-19')){
				$ret['msg'] = '线下充值已关闭，请到官网使用在线充值';
				$ret['code'] = 2;
				echo json_encode($ret);exit;
			}
			
			//线下 充值银行列表（原来写死的  为了方便我们以后修改 改成参数）
			$bankList = array(
				array(
					'bank_name' => '招商银行',
					'bank_branch'=> '深圳安联支行 ',
					'bank_username' => '汪杰',
					'bank_account' => '6214 8565 5127 1996'
				),
				array(
					'bank_name' => '工商银行',
					'bank_branch'=> '深圳福田支行 ',
					'bank_username' => '汪杰',
					'bank_account' => '6222 0840 0000 3794 222'
				),
			);
			$ret['bank_list'] = $bankList;
			$ret['code'] = 0;
			echo json_encode($ret);exit;
		}else{
			$ret['msg'] = '请先登录';
			$ret['code'] = 1;
			echo json_encode($ret);exit;
		}
	}
	
	//充值提交
	function submit(){
		//$user = $this->session->userdata('ych_user');
		$headers = apache_request_headers();
		$user_id = isset($headers['user_id'])? intval($headers['user_id']) : 0;
		$token = isset($headers['token'])? $headers['token'] : '';
		$token_key = 'token_'.$user_id;
		$token_val = $this->mycache->get($token_key);
		if($user_id && $token == $token_val){
			$type = 2;
			$money = $this->input->get_post('money');
			if(!is_numeric($money)){
				$ret['msg'] = '无效的金额';
				$ret['code'] = 2;
				echo json_encode($ret);exit;
			}else{
				$money = round($money,2);
				if ($money <= 0){
					$ret['msg'] = '无效的金额';
					$ret['code'] = 2;
					echo json_encode($ret);exit;
				}else{
					$payment = '0';
					$remark = $this->input->get_post('bankinfo').' 订单号：'. $this->input->get_post('remark');
					$fee = 0;
					$time = time();
					$recharge = array(
						'user_id' => $user_id,
						'trade_no' => $time.$user_id.rand(1,9),
						'fstatus' => 0,
						'money' => $money,
						'payment' => $payment,
						'freturn' => '',
						'ftype' => $type,
						'remark' => $remark,
						'fee' => $fee,
						'verify_userid' => 0,
						'verify_time' => $time,
						'verify_ip' => '',
						'verify_remark' => '',
						'add_time' => $time,
						'addip' => $this->input->ip_address()
					);
					$this->load->model('User_model', 'user');
					$userAll = $this->user->get_by_uid($user_id);
					if($userAll['real_status'] == 1){
						$this->load->model('Accountrecharge_model', 'recharge');
						$res = $this->recharge->add($recharge);
						if($res > 0){
							$ret['msg'] = '充值已提交，请等待审核';
							$ret['code'] = 0;
							echo json_encode($ret);exit;
						}else{
							$ret['msg'] = '提交失败';
							$ret['code'] = 4;
							echo json_encode($ret);exit;
						}
					}else{
						$ret['msg'] = '尚未实名认证';
						$ret['code'] = 3;
						echo json_encode($ret);exit;
					}
				}	
			}
		}else{
			$ret['msg'] = '请先登录';
			$ret['code'] = 1;
			echo json_encode($ret);exit;
		}
	}
}